// 百度地图API功能
var map = new BMap.Map("map");    // 创建Map实例
map.centerAndZoom(new BMap.Point(120.592014, 31.303584), 14);  // 初始化地图,设置中心点坐标和地图级别

//添加地图类型控件
map.addControl(new BMap.MapTypeControl({
	mapTypes:[
        BMAP_NORMAL_MAP,
        BMAP_HYBRID_MAP
    ]}));
map.setCurrentCity("郴州市");          // 设置地图显示的城市 此项是必须设置的
map.enableScrollWheelZoom(true);     //开启鼠标滚轮缩放
map.enableDragging();     //启用拖拽
setTimeout(function(){
   map.enableDragging();   //两秒后开启拖拽
   //map.enableInertialDragging();   //两秒后开启惯性拖拽
}, 2000);

/*-------------添加搜索--------------*/
function theLocation(IDname){
  
    var local, point;
      local = new BMap.LocalSearch(map, { //智能搜索
          onSearchComplete: fn
      });
    var cityName = $('#'+IDname).val();
      local.search(cityName);
   
      function fn() {
          //如果搜索的有结果
          //如果搜索的有结果
          if(local.getResults() != undefined) {
              map.clearOverlays(); //清除地图上所有覆盖物
              if(local.getResults().getPoi(0)) {
                  point = local.getResults().getPoi(0).point; //获取第一个智能搜索的结果
                  map.centerAndZoom(point, 18);
                  marker = new BMap.Marker(point); // 创建标注
                  map.addOverlay(marker); // 将标注添加到地图中
                  marker.enableDragging(); // 可拖拽
              } else {
                  alert("未匹配到地点!可拖动地图上的红色图标到精确位置");
              }
          } else {
              alert("未找到搜索结果")
          }
      }
}


/*--------------------添加标记-------------------------*/
//添加覆盖物主构造函数
function ComplexCustomOverlay(point, price, name, url){
  this._point = point;//添加的坐标
  this._price = price;//房价
  this._name = name;//房名
  this._url = url;//链接地址


 //  	var MAX = 10;
	// var markers = [];
	// var pt = null;
	// var i = 0;
	// for (; i < MAX; i++) {
	//    markers.push(new BMap.Marker(this._point));
	// }
	// //最简单的用法，生成一个marker数组，然后调用markerClusterer类即可。
	// var markerClusterer = new BMapLib.MarkerClusterer(map, {markers:markers});
}


//创建标记
ComplexCustomOverlay.prototype = new BMap.Overlay();
ComplexCustomOverlay.prototype.initialize = function(map){
  this._map = map;

  //创建主标记
  var div = this._div = document.createElement("div");
  div.style.position = "absolute";
  div.style.zIndex = BMap.Overlay.getZIndex(this._point.lat);
  div.style.backgroundColor = "#f00";
  div.style.border = "1px solid #f00";
  div.style.color = "white";
  div.style.height = "30px";
  div.style.padding = "3px";
  div.style.lineHeight = "24px";
  div.style.whiteSpace = "nowrap";
  div.style.MozUserSelect = "none";
  div.style.fontSize = "12px";
  div.style.display = 'inline-block';

  //添加主标记内容
  var span = this._span = document.createElement("span");
  div.appendChild(span);
  span.appendChild(document.createTextNode(this._price));//主标记价格

  

  //添加标记箭头
  var arrow = this._arrow = document.createElement("div");
  arrow.style.background = "url(http://map.baidu.com/fwmap/upload/r/map/fwmap/static/house/images/label.png) no-repeat";
  arrow.style.position = "absolute";
  arrow.style.width = "11px";
  arrow.style.height = "10px";
  arrow.style.top = "28px";
  arrow.style.left = "-2px";
  arrow.style.overflow = "hidden";
  div.appendChild(arrow);

  //添加付标记（hover 出现）
  var div2 = this._div2 = document.createElement("div");
  div2.style.position = "absolute";
  div2.style.zIndex = BMap.Overlay.getZIndex(this._point.lat);
  div2.style.backgroundColor = "#fff";
  div2.style.border = "1px solid #fff";
  div2.style.color = "#666";
  div2.style.height = "30px";
  div2.style.padding = "3px";
  div2.style.lineHeight = "24px";
  div2.style.whiteSpace = "nowrap";
  div2.style.MozUserSelect = "none";
  div2.style.fontSize = "12px";
  div2.style.display = 'none';

  //添加付标记内容
  var span = this._span = document.createElement("span");
  div2.appendChild(span);
  span.appendChild(document.createTextNode(this._name));//付标记房屋名称

  //添加主链接
  var a_link = this._a_link = document.createElement("a");
  a_link.href = this._url;//房屋链接
  a_link.appendChild(div);
  a_link.appendChild(div2);

  var that = this;//对象转换

  //鼠标移入事件函数
  div.onmouseover = function(){
    this.style.backgroundColor = "#00a966";
    this.style.borderColor = "#0f0";
    this.style.boxShadow = '0 0 3px rgba(0, 169, 102, 0.5)';
    this.style.zIndex = 100;//提高层级
    //this.getElementsByTagName("span")[0].innerHTML = that._price;
    arrow.style.backgroundPosition = "0px -20px";
    div2.style.display = 'inline-block';//显示隐藏的付标记
    div2.style.zIndex = 100;//提高层级
  }
  //鼠标移出事件函数
  div.onmouseout = function(){
    this.style.backgroundColor = "#f00";
    this.style.borderColor = "#f00";
    this.style.zIndex = 0;//恢复层级
    //this.getElementsByTagName("span")[0].innerHTML = that._price;
    arrow.style.backgroundPosition = "0px 0px";
    div2.style.display = 'none';//隐藏付标记
    div2.style.zIndex = 0;//恢复层级
  }

  map.getPanes().labelPane.appendChild(a_link);
  
  return a_link;
}

//标记位置定位及绘制
ComplexCustomOverlay.prototype.draw = function(){
  var map = this._map;
  var pixel = map.pointToOverlayPixel(this._point);
  this._div.style.left = pixel.x - parseInt(this._arrow.style.left) + "px";
  this._div.style.top  = pixel.y - 30 + "px";
  this._div2.style.left = pixel.x - parseInt(this._arrow.style.left)+ 90 + "px";
  this._div2.style.top  = pixel.y - 30 + "px";
}

//添加标记函数
function addMark(longitude,latitude,housingPrice,housingName,url){
	var txt = housingPrice+"元/m2套";//标注文字
    var myCompOverlay = new ComplexCustomOverlay(new BMap.Point(longitude,latitude),txt,housingName,url);
    map.addOverlay(myCompOverlay);
}